package com.lagou.work.worktwo;

import java.util.Scanner;

public class Gobang {

    private String[][] drawBoard(int x, int y, String[][] board,boolean flag){
        if (flag){
            board[x][y] = "1 ";
        }else {
            board[x][y] = "2 ";
        }
        System.out.println("\n--------------------------------------");
        for (int i = 0; i < board.length; i++) {
            for (int j = 0;  j < board[i].length; j++) {
                System.out.print(board[i][j] + " ");
            }
            System.out.println();
        }
        return board;
    }

    private void initBoard(String[][] board){
        System.out.println("棋盘初始化");
        String[] indStrs = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
        for(int i=0;i<=16;i++){
            if(i==0){
                System.out.print("  ");
                board[i][0] = "  ";
            }else{
                System.out.println();
                System.out.print(indStrs[i-1] + " ");
                board[i][0] = indStrs[i-1] + " ";
            }
            for(int j=0;j<16;j++){
                if(i==0){
                    board[i][j+1] = indStrs[j] + " ";
                    System.out.print(indStrs[j] + " ");
                }else{
                    board[i][j+1] = "+ ";
                    System.out.print("+ ");
                }
            }
        }
    }

    public void palyChess() {
        //声明下棋次数
        int cnt = 0;
        //声明可下棋子总数
        int total = 16*16;
        //声明标识用来区分黑白棋
        boolean flag = true;
        //声明下棋的棋盘数组
        String[][] board = new String[17][17];
        initBoard(board);
        System.out.println();
        for (int i=0;i<total;i++){
            if (cnt == total){
                System.out.println("棋盘已无位置");
                break;
            }
            Scanner scanner = new Scanner(System.in);
            if (flag){
                System.out.println("请黑方下棋(输入0-15中两个数字，并以空格隔开)");
            }else {
                System.out.println("请白方下棋(输入0-15中两个数字，并以空格隔开)");
            }
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            int[] checkPiece = checkPiece(x, y, board, scanner);
            x = checkPiece[0] + 1;
            y = checkPiece[1] + 1;
            board = drawBoard(x, y, board, flag);
            if (checkWin(x, y, board, flag)){
                if (flag){
                    System.out.println("黑棋赢了，恭喜你！！！");
                }else {
                    System.out.println("白棋赢了，恭喜你！！！");
                }
                break;
            }
            cnt++;
            flag = !flag;
        }
    }

    private int[] checkPiece(int x, int y, String[][] board, Scanner scanner){
        int[] parray = new int[2];
        while (x<0 || x>15 || y<0 || y>15){
            System.out.println("该位置超出棋盘范围，请重新下棋");
            x = scanner.nextInt();
            y = scanner.nextInt();
        }
        if ("1 ".equals(board[x+1][y+1]) || "2 ".equals(board[x+1][y+1])){
            System.out.println("该位置已有棋子,请重新下棋");
            x = scanner.nextInt();
            y = scanner.nextInt();
            checkPiece(x, y, board, scanner);
        }
        parray[0] = x;
        parray[1] = y;
        return parray;
    }

    private boolean checkWin(int x, int y, String[][] board, boolean flag){
        int minX = x - 4;
        int maxX = x + 4;
        int minY = y - 4;
        int maxY = y + 4;
        if (minX < 1){
            minX = 1;
        }
        if (maxX > 16){
            maxX = 16;
        }
        if (minY < 1){
            minY = 1;
        }
        if (maxY > 16){
            maxY = 16;
        }
        //初始化棋子出现次数
        int num1 = 0;
        //判断从定位左上角到右下角是否有符合条件的棋子
        int midY = minY;
        for (int i = minX; i <= maxX; i++) {
            int yInd = midY++;
            if (yInd > 16){
                break;
            }
            String piece = board[i][yInd];
            //System.out.println(i + " " + yInd + " left " + piece);
            num1 = getPieceNum(piece,num1,flag);
            if (num1>=5){
                return true;
            }
        }
        //将计数变量置为0
        int num2 = 0;
        //判断从定位右上角到左下角是否有符合条件的棋子
        midY = maxY;
        for (int i = minX ; i <= maxX; i++) {
            int yInd = midY--;
            if (yInd < 1){
                break;
            }
            String piece = board[i][yInd];
            //System.out.println(i + " " + yInd + " right" + piece);
            num2 = getPieceNum(piece, num2, flag);
            if (num2>=5){
                return true;
            }
        }
        //将计数变量置为0
        int num3 = 0;
        //判断从左到右是否有符合条件的棋子
        for (int i = minY; i<=maxY;i++){
            String piece = board[x][i];
            num3 = getPieceNum(piece, num3, flag);
            if (num3>=5){
                return true;
            }
        }
        //将计数变量置为0
        int num4 = 0;
        //判断从上到下是否有符合条件的棋子
        for (int i = minX; i<=maxX; i++){
            String piece = board[i][y];
            num4 = getPieceNum(piece, num4, flag);
            if (num4>=5){
                return true;
            }
        }
        return false;
    }

    private int getPieceNum(String piece, int num, boolean flag){
        if (flag){
            if ("1 ".equals(piece)){
                num++;
            }else {
                num = 0;
            }
        }else {
            if ("2 ".equals(piece)){
                num++;
            }else {
                num = 0;
            }
        }
        return num;
    }

}
